package com.h2.gwt.util.activity;

import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.user.client.Timer;

public class ActivityMonitor {
   
   private Boolean _userActive = Boolean.FALSE;
   
   /** Default is every 2 seconds. */
   private Integer _eventTime;
   
   /** Default threshold is 5 minutes. */
   private Integer _threshold;
   
   private Long _lastEventTime;
   
   private MouseMoveHandler _eventHandler;
   
   ////
   ////
   
   public ActivityMonitor() {
      //nothing to do
   }
   
   public void init() {
      
      setUserActive(Boolean.FALSE);
      //2 seconds
      setEventTime(2000);
      
      setThreshold(5 * 60 * 1000);
      setLastEventTime(System.currentTimeMillis());
      
      setEventHandler(new MouseMoveHandler() {
         @Override
         public void onMouseMove(MouseMoveEvent event) {
            if(!isUserActive()) {
               setUserActive(Boolean.TRUE);
            }
            setLastEventTime(System.currentTimeMillis());
         }
      });
      
//      addLongPollingTimer();
   }
   
//   private void addLongPollingTimer() {
//      Timer timer = new Timer() {
//         public void run() {
//            setUserActive(Boolean.TRUE);
//            setLastEventTime(System.currentTimeMillis());
//         }
//      };
//      timer.scheduleRepeating(getLongTime());
//      timer.run();
//   }
   
//   public void addEventTimer() {
//      Timer timer = new Timer() {
//         public void run() {
//            //if last time is greater than threshold, then user isn't active
//            if(isUserActive() && System.currentTimeMillis()
//                  - getLastEventTime() > getThreshold()) {
//               setUserActive(Boolean.FALSE);
//            }
//         }
//      };
//      timer.scheduleRepeating(getEventTime());
//      timer.run();
//   }
   
   public Boolean isUserActive(Integer threshold) {
      long diff = System.currentTimeMillis() - getLastEventTime();
      return (System.currentTimeMillis() - getLastEventTime()) < threshold;
   }

   /**
    * @return the userActive
    */
   public Boolean isUserActive() {
      return _userActive;
   }

   /**
    * @param userActive the userActive to set
    */
   public void setUserActive(Boolean userActive) {
      _userActive = userActive;
   }

   /**
    * @return the eventTime
    */
   public Integer getEventTime() {
      return _eventTime;
   }

   /**
    * @param eventTime the eventTime to set
    */
   public void setEventTime(Integer eventTime) {
      _eventTime = eventTime;
   }

   /**
    * @return the lastEventTime
    */
   public Long getLastEventTime() {
      return _lastEventTime;
   }

   /**
    * @param lastEventTime the lastEventTime to set
    */
   public void setLastEventTime(Long lastEventTime) {
      _lastEventTime = lastEventTime;
   }

   /**
    * @return the threshold
    */
   public Integer getThreshold() {
      return _threshold;
   }

   /**
    * @param threshold the threshold to set
    */
   public void setThreshold(Integer threshold) {
      _threshold = threshold;
   }

   /**
    * @return the boardEventHandler
    */
   public MouseMoveHandler getEventHandler() {
      return _eventHandler;
   }

   /**
    * @param boardEventHandler the boardEventHandler to set
    */
   public void setEventHandler(MouseMoveHandler eventHandler) {
      _eventHandler = eventHandler;
   }
}
